#!/usr/bin/env python
# encoding: utf-8
"""
fastxmlsearch.py

This script use the fastxmlsearch module and illustrates 
use cases to build and query indexes from XML corpora.

"""

from fastxmlsearch import IndexManager, DataManager, QueryManager, settings

from examples.parser import parseCustomFile

try:
	import cProfile
except:
	print 'no cProfile'


def build():
    
    #corpus=DataManager.Corpus('hgc')                                   # switched to DATA_PATH 
    #corpus.register()                                                  # only needed on the first run
    
    index=IndexManager.Index('hgc', 'positional', 'hgc10k.txt')         # Index object with meta infos
    
    for x in parseCustomFile(index.indexSource):                        # single pass
        index.updateLexicon( (x[0].group(3), x[1]))
        index.updatePositionalIndex((x[0].group(3), 1, x[1]))
        index.updateLexicon( (x[0].group(4), x[1]))
        index.updatePositionalIndex((x[0].group(4), 1, x[1]))
        
        if x[0].group(3) != x[0].group(5):                              # only if lemma != word
            index.updateLexicon( (x[0].group(5), x[1]))
            index.updatePositionalIndex((x[0].group(5), 1, x[1]))
        else:
            pass
    
    index.save()                                                        # index gets saved to disc
    
    print "Index ready!"


def query():
	query = QueryManager.Query('_hgc_positional','ART ~3 NN')
	print query.result


if __name__ == "__main__":
    
	#cProfile.run('build()')
	#build()
	
	#cProfile.run('query()')
	query()